package com.maxcion.scalelayoutmanager.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


abstract class PageLoadRecyclerVewAdapter<T>(dataList: List<T>?) :
    RecyclerView.Adapter<BaseViewHolder?>() {
    protected var mContext: Context? = null
    protected var mDataList: MutableList<T>?
    private var mStopLoadMore = false
    private var recyclerView: RecyclerView? = null
    private var mEnableLoadMore = false
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        if (mContext == null) {
            mContext = parent.context
        }
        var holder: BaseViewHolder? = null
        holder = getBaseViewHolderByType(parent, viewType)
        return holder
    }

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        convert(holder, position, getItem(position))
    }

    protected abstract fun convert(holder: BaseViewHolder?, position : Int ,item: T?)
    override fun getItemCount(): Int {
        return if (mDataList == null) 0 else mDataList!!.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (position >= mDataList!!.size) LOADING_VIEW else getDefItemViewType(position)
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        this.recyclerView = recyclerView
        val layoutManager = recyclerView.layoutManager
        if (layoutManager is GridLayoutManager) {
            val gridLayoutManager = layoutManager
            val spanSizeLookup = gridLayoutManager.spanSizeLookup
            val spanCount = gridLayoutManager.spanCount
            if (spanSizeLookup != null) {
                gridLayoutManager.spanSizeLookup = object : SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int {
                        return if (getItemViewType(position) == LOADING_VIEW) spanCount else spanSizeLookup.getSpanSize(
                            position
                        )
                    }
                }
            }
        }
    }

    /* start********************************************对外接口*********************************************/

    fun setNewData(newDataList: List<T>) {
        val datas = newDataList ?: ArrayList()
        mDataList!!.clear()
        mDataList!!.addAll(datas)
        notifyDataSetChanged()
    }

    fun addDataList(dataList: List<T>?) {
        if (dataList != null && dataList.size > 0) {
            val dataSize = dataSize
            mDataList!!.addAll(dataList)
            notifyItemRangeInserted(dataSize, dataList.size)
        }
    }

    val dataList: List<T>?
        get() = mDataList


    fun getItem(position: Int): T? {
        return if (position >= 0 && position < mDataList!!.size) mDataList!![position] else null
    }

    val dataSize: Int
        get() = mDataList!!.size


    /* end********************************************对外接口*********************************************/ /* start********************************************内部使用函数*********************************************/


    /**
     * 判断当前数据是否超过一屏幕
     *
     * @Date: 2020/6/3 11:19
     */
    val isFullScreen: Boolean
        get() {
            var result = true
            if (recyclerView != null) {
                val layoutManager = recyclerView!!.layoutManager
                if (layoutManager is LinearLayoutManager) {
                    val linearLayoutManager = layoutManager
                    val lastCompletelyVisibleItemPosition =
                        linearLayoutManager.findLastCompletelyVisibleItemPosition()
                    val firstCompletelyVisibleItemPosition =
                        linearLayoutManager.findFirstCompletelyVisibleItemPosition()
                    val itemCount = itemCount
                    result =
                        firstCompletelyVisibleItemPosition != 0 || lastCompletelyVisibleItemPosition + 1 < itemCount
                }
            }
            return result
        }


    /* end********************************************内部使用函数*********************************************/
    protected open fun getDefItemViewType(position: Int): Int {
        return super.getItemViewType(position)
    }

    protected abstract val itemLayoutId: Int

    protected open fun getBaseViewHolderByType(parent: ViewGroup, viewType: Int): BaseViewHolder {
        val holder: BaseViewHolder
        holder = BaseViewHolder.getHolder(
            parent.context, parent,
            itemLayoutId
        )
        return holder
    }

    companion object {
        protected const val LOADING_VIEW = 0x00000111
    }

    init {
        mDataList = (dataList ?: ArrayList()) as MutableList<T>?
    }
}